package com.nus.fellowmark.modules.module.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.nus.fellowmark.common.response.ServerResponseEntity;
import com.nus.fellowmark.entity.EnrollmentEntity;
import com.nus.fellowmark.entity.ModuleEntity;
import com.nus.fellowmark.entity.UserEntity;
import com.nus.fellowmark.modules.module.dto.EnrollmentDTO;
import com.nus.fellowmark.modules.module.service.EnrollmentService;
import com.nus.fellowmark.modules.module.service.ModuleService;
import com.nus.fellowmark.modules.user.service.UserService;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.nus.fellowmark.common.utils.PageUtils;


/**
 * 
 *
 * @author Chen Ruoyi
 * @email chenruoyisz@gmail.com
 * @date 2023-02-10  
 */
@RestController
@RequestMapping("enrollment")
@CrossOrigin
public class EnrollmentController {
    @Autowired
    private EnrollmentService enrollmentService;

    @Autowired
    private ModuleService moduleService;

    @Autowired
    private UserService userService;


    @ApiOperation("list enrollments")
    @RequestMapping("/list")
    public ServerResponseEntity list(@RequestParam Map<String, Object> params){
        PageUtils page = enrollmentService.queryPage(params);

        return ServerResponseEntity.success("page", page);
    }


    @ApiOperation("select enrollment by id ")
    @RequestMapping("/info/{id}")
    public ServerResponseEntity info(@PathVariable("id") Integer id){
		EnrollmentEntity enrollment = enrollmentService.getById(id);

        return ServerResponseEntity.success("enrollment", enrollment);
    }

    @SaCheckRole(value = {"stu"}, mode = SaMode.OR)
    @ApiOperation("select modules enrolled by student_id ")
    @RequestMapping("/student/{id}")
    public ServerResponseEntity selectByStuId(@PathVariable("id") Integer id){
		List<EnrollmentEntity> enrollment = enrollmentService.getByStudentId(id);
        List<ModuleEntity> modulesEnrolled = new ArrayList<ModuleEntity>();
        for(EnrollmentEntity e : enrollment){
            modulesEnrolled.add(moduleService.getById(e.getModuleId()));
        }
        return ServerResponseEntity.success("modulesEnrolled", modulesEnrolled);
    }

    @SaCheckRole(value = {"staff", "admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("select all students enrolled in module_id ")
    @RequestMapping("/module/{id}")
    public ServerResponseEntity selectByModuleId(@PathVariable("id") Integer id){
		List<Integer> enrollment = enrollmentService.getStudentIdsInModule(id);
        List<UserEntity> studentsEnrolled = new ArrayList<UserEntity>();
        for(Integer e : enrollment){
            studentsEnrolled.add(userService.getByStudentId(e));
        }
        return ServerResponseEntity.success("studentsEnrolled", studentsEnrolled);
    }

    @SaCheckRole(value = {"staff", "admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("save enrollment")
    @RequestMapping("/save")
    public ServerResponseEntity save(@RequestBody EnrollmentDTO enrollmentDTO){
        // check if student email exists
        if (userService.getByEmail(enrollmentDTO.getEmail()) == null) {
            return ServerResponseEntity.showFailMsg(enrollmentDTO.getEmail() + " does not exist!");
        }

        Integer moduleId = enrollmentDTO.getModuleId();
        Integer userId = userService.getByEmail(enrollmentDTO.getEmail()).getId();

        // check if student is already enrolled in this module
        EnrollmentEntity enrollmentExist = enrollmentService.getByStudentIdAndModuleId(userId, moduleId);
        if (enrollmentExist == null) {
            EnrollmentEntity enrollment = new EnrollmentEntity();
            enrollment.setModuleId(moduleId);
            enrollment.setStudentId(userId);
            enrollmentService.save(enrollment);
            return ServerResponseEntity.success();
        }
        else {
            return ServerResponseEntity.showFailMsg(enrollmentDTO.getEmail() + " is already enrolled!");
        }
    }

    @ApiOperation("update enrollment")
    @RequestMapping("/update")
    public ServerResponseEntity update(@RequestBody EnrollmentEntity enrollment){
		enrollmentService.updateById(enrollment);

        return ServerResponseEntity.success();
    }

    @SaCheckRole(value = {"staff", "admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("delete enrollment")
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody EnrollmentDTO enrollmentDTO){
		UserEntity student = userService.getByEmail(enrollmentDTO.getEmail());
        EnrollmentEntity enrollment = enrollmentService.getByStudentIdAndModuleId(student.getId(), enrollmentDTO.getModuleId());
        enrollmentService.removeById(enrollment.getId());
        return ServerResponseEntity.success();
    }

}
